package com.www.applyword.base;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.os.Handler;

import androidx.multidex.MultiDex;

import com.mob.MobSDK;
import com.orhanobut.logger.LogLevel;
import com.orhanobut.logger.Logger;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.tencent.bugly.Bugly;
import com.tencent.imsdk.TIMSdkConfig;
import com.tencent.imsdk.session.SessionWrapper;
import com.tencent.qcloud.tim.uikit.TUIKit;
import com.tencent.qcloud.tim.uikit.config.CustomFaceConfig;
import com.tencent.qcloud.tim.uikit.config.GeneralConfig;
import com.tencent.qcloud.tim.uikit.config.TUIKitConfigs;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 *
 * @author Administrator
 */
public class BaseApplication extends Application {

    public static Map<String, Long> map;
    private static Context context;
    private static BaseApplication mInstance;
    /**
     * 主线程ID
     */
    private static int mMainThreadId = -1;
    /**
     * 主线程ID
     */
    private static Thread mMainThread;

    /**
     * 主线程Handler
     */
    private static Handler mMainThreadHandler;

    private static List<Activity> records = new ArrayList<>();

    public static Context getContext() {
        return context;
    }

    public static BaseApplication getApplication() {
        return mInstance;
    }

    /**
     * 获取主线程ID
     */
    public static int getMainThreadId() {
        return mMainThreadId;
    }

    /**
     * 获取主线程
     */
    public static Thread getMainThread() {
        return mMainThread;
    }

    /**
     * 获取主线程的handler
     */
    public static Handler getMainThreadHandler() {
        return mMainThreadHandler;
    }

    public static void perfectExit() {
        for (Activity activity : records) {
            activity.finish();
        }
    }

    //static 代码段可以防止内存泄露
    static {
        //设置全局的Header构建器
        SmartRefreshLayout.setDefaultRefreshHeaderCreator((context, layout) -> {
            //全局设置主题颜色
            return new ClassicsHeader(context);
        });
        //设置全局的Footer构建器
        SmartRefreshLayout.setDefaultRefreshFooterCreator((Context context, RefreshLayout layout) -> {
            //指定为经典Footer，默认是 BallPulseFooter
            return new ClassicsFooter(context);
        });
    }


    @Override
    public void onCreate() {
        super.onCreate();
        context = getApplicationContext();
        mMainThreadId = android.os.Process.myTid();
        mMainThread = Thread.currentThread();
        mMainThreadHandler = new Handler();
        mInstance = this;
        MobSDK.init(this);

        Bugly.init(getApplicationContext(), "d2e37ae659", false);

        //判断是否是在主线程
       if (SessionWrapper.isMainProcess(getApplicationContext())) {
            // 配置 Config，请按需配置
            TUIKitConfigs configs = TUIKit.getConfigs();
            //1400275963
            configs.setSdkConfig(new TIMSdkConfig(1400275763));
            configs.setCustomFaceConfig(new CustomFaceConfig());
            configs.setGeneralConfig(new GeneralConfig());
            TUIKit.init(this, 1400275763, configs);
        }

            Logger.init("hhh")
                    .methodOffset(2)
                    .methodCount(2)
                    .hideThreadInfo()
                    .logLevel(LogLevel.FULL);
    }


    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        // you must install multiDex whatever tinker is installed!
        MultiDex.install(base);
    }

    public void addActvity(Activity activity) {
        records.add(activity);
    }

    public void removeActvity(Activity activity) {
        records.remove(activity);
    }
}
